<?php
/**
 * 菜单模型类
 * @Author hugo
 * @Email webmaster@jushare.com
 */
namespace Cpanel\Model;
use Common\Model\CommonModel;

class MenuModel extends CommonModel{

    /**
     * 读取树型数据
     * @return mixed
     */
    public function fetchMenu($isMenu = false, $fetchParent = false, $pid = 0)
    {
        $this->alias('m');
        if($isMenu)
        {
            $this->join("__MENU__ AS mr ON mr.id = m.pid", 'LEFT');
            $this->field("m.*, m.id AS mid, if(m.level = 0 , m.name, concat(mr.name, '/', m.name)) AS url");
            $this->where("m.display = 1");
        }else{
            $this->field("m.*, m.id AS mid");
        }
        if($fetchParent)
        {
            $this->where("m.pid = $pid");
        }
        $data = $this
            ->order("m.pid ASC, m.orderby DESC")
            ->select();
        if($data){
            foreach($data as $key => $row){
                $row['query'] = (empty($row['querys']) ? 'id=' : $row['querys'] . '&id=') . $row['id'];
                $row['href'] = formatUrl($row['url'], $row['query']);
                $row['attributes'] = json_encode($row);
                $data[$key] = $row;
            }
        }
        return $data;
    }
}